package ciyuan.Gui.Screen;

import ciyuan.DataManager.PlayerDataManager;
import ciyuan.Gui.Component.AddPointWidget;
import ciyuan.Gui.Component.StatusWidget;
import ciyuan.MainClass;
import ciyuan.Network.ContainerOpenPacket;
import ciyuan.Network.NetWorkRegister;
import ciyuan.Player.ComputedPlayerInfo;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.inventory.InventoryScreen;
import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TranslationTextComponent;

import static ciyuan.Constants.PlayerDataType.*;

public class PlayerInfoScreen extends Screen {

    private static final ResourceLocation PLAYER_INFO_SCREEN_RESOURCE = new ResourceLocation(MainClass.MODID, "textures/gui/player_info/playerinfo.png");
    private static final TranslationTextComponent TITLE = new TranslationTextComponent("ciyuan.gui.title2");
    private static final int imageWidth = 250;
    private static final int imageHeight = 166;

    private int left;
    private int top;

    public PlayerInfoScreen() {
        super(TITLE);
        this.addMenuButtons();
    }

    @Override
    protected void init() {
        this.left = (this.width - imageWidth) / 2;
        this.top = (this.height - imageHeight) / 2;

        this.addStatusWidget();
        this.addPointWidget();
    }

    private void addStatusWidget() {
        this.addButton(new StatusWidget(this.left + 20, this.top + 60, player -> String.valueOf(player.getHealth()), "health"));
        this.addButton(new StatusWidget(this.left + 60, this.top + 60, player -> String.valueOf(player.getMaxHealth()), "max_health"));
        this.addButton(new StatusWidget(this.left + 100, this.top + 60, player -> String.valueOf(PlayerDataManager.getLong(null, MAGIC)), "magic"));
        this.addButton(new StatusWidget(this.left + 140, this.top + 60, player -> String.valueOf(PlayerDataManager.getLong(null, MAX_MAGIC)), "max_magic"));
        this.addButton(new StatusWidget(this.left + 180, this.top + 60, player -> String.valueOf(ComputedPlayerInfo.getDefence(player)), "defence"));

        this.addButton(new StatusWidget(this.left + 20, this.top + 82, player -> String.valueOf(ComputedPlayerInfo.getMeleeDmage(player)), "melee_damage"));
        this.addButton(new StatusWidget(this.left + 60, this.top + 82, player -> String.valueOf(ComputedPlayerInfo.getRemoteDmage(player)), "remote_damage"));
        this.addButton(new StatusWidget(this.left + 100, this.top + 82, player -> String.valueOf(ComputedPlayerInfo.getMagicDamage(player)), "magic_damage"));
        this.addButton(new StatusWidget(this.left + 140, this.top + 82, player -> String.valueOf(ComputedPlayerInfo.getMagicRegen(player)), "magic_regen"));
        this.addButton(new StatusWidget(this.left + 180, this.top + 82, player -> String.valueOf(ComputedPlayerInfo.getExpBonus(player)), "exp_bonus"));

        this.addButton(new StatusWidget(this.left + 20, this.top + 104, player -> String.valueOf(ComputedPlayerInfo.getCritChance(player)), "crit_chance"));
        this.addButton(new StatusWidget(this.left + 60, this.top + 104, player -> String.valueOf(ComputedPlayerInfo.getCritDamage(player)), "crit_damage"));
        this.addButton(new StatusWidget(this.left + 100, this.top + 104, player -> String.valueOf(ComputedPlayerInfo.getSearchAbility(player)), "search_ability"));
        this.addButton(new StatusWidget(this.left + 140, this.top + 104, player -> String.valueOf(PlayerDataManager.getLong(null, STAMNA)), "stamina"));
        this.addButton(new StatusWidget(this.left + 180, this.top + 104, player -> String.valueOf(ComputedPlayerInfo.getMaxStamina(player)), "max_stamina"));
    }

    private void addPointWidget() {
        this.addButton(new AddPointWidget(this.left + 70, this.top + 5, STRENGTH));
        this.addButton(new AddPointWidget(this.left + 90, this.top + 5, STRONG));
        this.addButton(new AddPointWidget(this.left + 110, this.top + 5, AGILE));
        this.addButton(new AddPointWidget(this.left + 130, this.top + 5, WISDOM));
        this.addButton(new AddPointWidget(this.left + 150, this.top + 5, ANALYSIS));
        this.addButton(new AddPointWidget(this.left + 170, this.top + 5, FLEXIBLE));
    }

    private void addMenuButtons() {
        this.addButton(new Button(left, top + 32, 20, 8, new TranslationTextComponent("ciyuan.gui.playerInfo.label.addPoint"), (Button) -> {
            NetWorkRegister.INSTANCE.sendToServer(new ContainerOpenPacket("OrnamentsScreen"));
        }));
    }

    @Override
    public void renderBackground(MatrixStack matrixStack) {
        super.renderBackground(matrixStack);

        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bind(PLAYER_INFO_SCREEN_RESOURCE);
        blit(matrixStack, this.width / 2 - imageWidth / 2, 20, 0, 0, imageWidth, imageHeight, imageWidth, imageHeight);
    }

    @Override
    public void render(MatrixStack matrixStack, int mouseX, int mouseY, float particleTick) {
        this.renderBackground(matrixStack);

        ClientPlayerEntity player = Minecraft.getInstance().player;
        drawString(matrixStack, this.minecraft.font, new TranslationTextComponent("ciyuan.gui.playerInfo.label.attrbutePoints"), left + 70, top - 6, 16777215);
        drawString(matrixStack, this.minecraft.font, String.valueOf(PlayerDataManager.getInt(player, ATTRBUTE_POINTS)), left + 115, top - 6, 16777215);

        InventoryScreen.renderEntityInInventory((this.width - imageWidth) / 2 + 40, (this.height - imageHeight) / 2 + 45, 25, (this.width - imageWidth) / 2 - mouseX, (this.height - imageHeight) / 2 - mouseY, Minecraft.getInstance().player);
        super.render(matrixStack, mouseX, mouseY, particleTick);
    }

}
